/***************************************************************************************
 The copyright in this software is being made available under the BSD License, included
 below. This software may be subject to other third party rights,including patent rights,
 and no such rights are granted under this license.

 Regents of the Tsinghua University, Peking University, Zhejiang University, HKUST
 Tsinghua University, Peking University, Zhejiang University, HKUST 2011

 Copyright (c) 2011, Regents of the Tsinghua University, Peking University, Zhejiang
 University, HKUST. All Rights Reserved.

 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:
   * Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright notice,
     this list of conditions and the following disclaimer in the documentation
     and/or other materials provided with the distribution.
   * Neither the names of the copyright holders nor the names of its
     contributors may be used to endorse or promote products derived from this
     software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 OF SUCH DAMAGE.
***************************************************************************************/

/**************************************************************************************
* File name: bitstream.c
* Function: decode bitstream
*
*************************************************************************************
*/

#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "contributors.h"
#include "global.h"
#include "memalloc.h"

unsigned char bit[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };



input_bitstream_t IRABS,*pIRABS = &IRABS;


void open_input_bitstream( char *fn )
{
    pIRABS->f = fopen( fn, "rb" );
    if ( pIRABS->f == NULL )
    {
        printf( "\nCan't open file %s", fn );
        exit( -1 );
    }

    pIRABS->uClearBits = 0xffffffff;
    pIRABS->iBytePosition = 0;
    pIRABS->iBufBytesNum = 0;
    pIRABS->iClearBitsNum = 0;
    pIRABS->iStuffBitsNum = 0;
    pIRABS->iBitsCount = 0;
    pIRABS->uPre3Bytes = 0;
}

void close_input_bitstream()
{
    fclose( pIRABS->f );
}

/*
*************************************************************************
* Function: move iBytePosition to the next byte of start code prefix
* Input:
* Output:
* Return:  0 : OK
-1 : arrive at stream end and start code is not found
-2 : p->iBytePosition error
* Attention:
*************************************************************************
*/
int next_start_code( input_bitstream_t * p )
{
    int i, m;
    unsigned char a=0, b=0;           // a b 0 1 2 3 4 ... M-3 M-2 M-1
    m = 0;                        // cjw 20060323 for linux envi

    while ( 1 )
    {
        if ( p->iBytePosition >= p->iBufBytesNum - 2 )      //if all bytes in buffer has been searched
        {
            m = p->iBufBytesNum - p->iBytePosition;
            if ( m < 0 )
                return -2;              // p->iBytePosition error
            if ( m == 1 )
                b = p->buf[p->iBytePosition + 1];
            if ( m == 2 )
            {
                b = p->buf[p->iBytePosition + 1];
                a = p->buf[p->iBytePosition];
            }
            p->iBufBytesNum = fread( p->buf, 1, SVA_STREAM_BUF_SIZE, p->f );
            p->iBytePosition = 0;
        }

        if ( p->iBufBytesNum + m < 3 )
            return -1;                //arrive at stream end and start code is not found

        if ( m == 1 && b == 0 && p->buf[0] == 0 && p->buf[1] == 1 )
        {
            p->iBytePosition = 2;
            p->iClearBitsNum = 0;
            p->iStuffBitsNum = 0;
            p->iBitsCount += 24;
            p->uPre3Bytes = 1;
            return 0;
        }

        if ( m == 2 && b == 0 && a == 0 && p->buf[0] == 1 )
        {
            p->iBytePosition = 1;
            p->iClearBitsNum = 0;
            p->iStuffBitsNum = 0;
            p->iBitsCount += 24;
            p->uPre3Bytes = 1;
            return 0;
        }

        if ( m == 2 && b == 0 && p->buf[0] == 0 && p->buf[1] == 1 )
        {
            p->iBytePosition = 2;
            p->iClearBitsNum = 0;
            p->iStuffBitsNum = 0;
            p->iBitsCount += 24;
            p->uPre3Bytes = 1;
            return 0;
        }

        for ( i = p->iBytePosition; i < p->iBufBytesNum - 2; i++ )
        {
            if ( p->buf[i] == 0 && p->buf[i + 1] == 0 && p->buf[i + 2] == 1 )
            {
                p->iBytePosition = i + 3;
                p->iClearBitsNum = 0;
                p->iStuffBitsNum = 0;
                p->iBitsCount += 24;
                p->uPre3Bytes = 1;
                return 0;
            }
            p->iBitsCount += 8;
        }
        p->iBytePosition = i;
    }
}

/*
*************************************************************************
* Function:
* Input:
* Output:
* Return:  0 : OK
-1 : arrive at stream end
-2 : meet another start code
* Attention:
*************************************************************************
*/
int clear_next_byte( input_bitstream_t * p )
{
    int i, k, j;
    unsigned char temp[3];
    i = p->iBytePosition;
    k = p->iBufBytesNum - i;
    if ( k < 3 )
    {
        for ( j = 0; j < k; j++ )
            temp[j] = p->buf[i + j];
        p->iBufBytesNum = fread( p->buf + k, 1, SVA_STREAM_BUF_SIZE - k, p->f );
        if ( p->iBufBytesNum == 0 )
        {
            if ( k > 0 )
            {
                p->uPre3Bytes = ( ( p->uPre3Bytes << 8 ) | p->buf[i] ) & 0x00ffffff;
                if ( p->uPre3Bytes < 4 && demulate_enable )
                {
                    p->uClearBits = ( p->uClearBits << 6 ) | ( p->buf[i] >> 2 );
                    p->iClearBitsNum += 6;
                }
                else
                {
                    p->uClearBits = ( p->uClearBits << 8 ) | p->buf[i];
                    p->iClearBitsNum += 8;
                }
                p->iBytePosition++;
                return 0;
            }
            else
            {
                return -1;              //arrive at stream end
            }
        }
        else
        {
            for ( j = 0; j < k; j++ )
                p->buf[j] = temp[j];
            p->iBufBytesNum += k;
            i = p->iBytePosition = 0;
        }
    }
    if ( p->buf[i] == 0 && p->buf[i + 1] == 0 && p->buf[i + 2] == 1 )
        return -2;                  // meet another start code
    p->uPre3Bytes = ( ( p->uPre3Bytes << 8 ) | p->buf[i] ) & 0x00ffffff;
    if ( p->uPre3Bytes < 4 && demulate_enable )
    {
        p->uClearBits = ( p->uClearBits << 6 ) | ( p->buf[i] >> 2 );
        p->iClearBitsNum += 6;
    }
    else
    {
        p->uClearBits = ( p->uClearBits << 8 ) | p->buf[i];
        p->iClearBitsNum += 8;
    }
    p->iBytePosition++;
    return 0;
}

/*
*************************************************************************
* Function:
* Input:
* Output:
* Return:  0 : OK
-1 : arrive at stream end
-2 : meet another start code
* Attention:
*************************************************************************
*/
int read_n_bit( input_bitstream_t * p, int n, int *v )
{
    int r;
    unsigned int t;
    while ( n > p->iClearBitsNum )
    {
        r = clear_next_byte( p );
        if ( r )
        {
            if ( r == -1 )
            {
                if ( p->iBufBytesNum - p->iBytePosition > 0 )
                    break;
            }
            return r;
        }
    }
    t = p->uClearBits;
    r = 32 - p->iClearBitsNum;
    *v = ( t << r ) >> ( 32 - n );
    p->iClearBitsNum -= n;
    return 0;
}


/*
*************************************************************************
* Function:For supporting multiple sequences in a stream
* Input:
* Output:
* Return:
* Attention: Modified by X ZHENG, 20080721
* Author: Carmen, 2008/01/22
*************************************************************************
*/
int is_end_of_bitstream()
{
    int ret, seof, m;
    ret = feof( pIRABS->f );
    m = pIRABS->iBufBytesNum - pIRABS->iBytePosition;
    seof = ( ( ret ) && ( !pIRABS->iBufBytesNum ) ) || ( ( ret ) && ( m == 1 ) );
    return ( ( !seof ) ? ( 0 ) : ( 1 ) );
}


/*
*************************************************************************
* Function:check slice start code
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
int check_start_code()
{
    int temp_i, temp_val;
    if ( currStream->bitstream_length * 8 - currStream->frame_bitoffset == 0 )
        return 1;

    if ( img->current_mb_nr == 0 )
    {
        if ( currStream->bitstream_length * 8 - currStream->frame_bitoffset <= 8 && currStream->bitstream_length * 8 - currStream->frame_bitoffset > 0 )
        {
            temp_i = currStream->bitstream_length * 8 - currStream->frame_bitoffset;
            assert( temp_i > 0 );
            temp_val = get_uv( temp_i, "filling data" );
        }
    }

    if ( img->current_mb_nr == 0 )
    {
        if ( StartCodePosition > 4 && StartCodePosition < 0x000fffff )
            return 1;
        else
        {
            currStream->frame_bitoffset = currentbitoffset;
            return 0;
        }
    }

    if ( img->current_mb_nr != 0 )
    {
        if ( currStream->bitstream_length * 8 - currStream->frame_bitoffset <= 8 && currStream->bitstream_length * 8 - currStream->frame_bitoffset > 0 )
        {
            temp_i = currStream->bitstream_length * 8 - currStream->frame_bitoffset;
            assert( temp_i > 0 );
            temp_val = get_uv( temp_i, "stuffing pattern" );
            if ( temp_val == ( 1 << ( temp_i - 1 ) ) && img->cod_counter <= 0 )
                return 1;
        }
        return 0;
    }

    return 1;
}

/*
*************************************************************************
* Function: Check start code's type
* Input:
* Output:
* Return:
* Attention:

* Author:
*************************************************************************
*/
void CheckType( int startcode )
{
    startcode = startcode & 0x000000ff;
    switch ( startcode )
    {
        case 0xb0:
        case 0xb2:
        case 0xb5:
            demulate_enable = 0;
            break;
        default:
            demulate_enable = 1;
            break;
    }
}

int get_one_unit( char *buf, int *startcodepos, int *length )
{
    int i, j, k;
    i = next_start_code( pIRABS );
    if ( i != 0 )
    {
        if ( i == -1 )
            printf( "\narrive at stream end and start code is not found!" );
        if ( i == -2 )
            printf( "\np->iBytePosition error!" );
        exit( i );
    }
    buf[0] = 0;
    buf[1] = 0;
    buf[2] = 1;
    *startcodepos = 3;
    i = read_n_bit( pIRABS, 8, &j );
    buf[3] = ( char )j;
    CheckType( buf[3] );
    if ( buf[3] == SEQUENCE_END_CODE )
    {
        *length = 4;
        return -1;
    }
    k = 4;
    while ( 1 )
    {
        i = read_n_bit( pIRABS, 8, &j );
        if ( i < 0 )
            break;
        buf[k++] = ( char )j;
    }
    if ( pIRABS->iClearBitsNum > 0 )
    {
        int shift;
        shift = 8 - pIRABS->iClearBitsNum;
        i = read_n_bit( pIRABS, pIRABS->iClearBitsNum, &j );

        if ( j != 0 )
            buf[k++] = ( char )( j << shift );
    }
    *length = k;
    return k;
}
